package com.alibaba.citrus.cr.platform.enhance.repository.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.cr.platform.enhance.repository.DiffErrorRepository;
import com.alibaba.citrus.cr.platform.enhance.repository.model.diff.common.DiffErrorDTO;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.crplatformenhance.differror.dto.BatchSaveDiffErrorListRequest;
import com.epoch.app.crplatformenhance.differror.model.DiffError;
import com.epoch.app.crplatformenhance.differror.service.DiffErrorService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

/**
 * @author: qiaojiange
 * @create: 2022/01/19
 * @Description 对账差错资源层
 */
@Repository
public class DiffErrorRepositoryImpl implements DiffErrorRepository {

    @Resource
    private DiffErrorService diffErrorService;

    @Override
    @RepositoryInvoker
    public Boolean batchSaveDiffErrorList(List<DiffErrorDTO> diffErrorDTOList) {
        if (CollectionUtils.isEmpty(diffErrorDTOList)) {
            return false;
        }
        List<DiffError> diffErrorList = new ArrayList<>();
        diffErrorDTOList.forEach(dto -> {
            DiffError diffError = new DiffError();
            BeanUtils.copyProperties(dto, diffError);
            diffErrorList.add(diffError);
        });

        BatchSaveDiffErrorListRequest request = new BatchSaveDiffErrorListRequest();
        request.setArray(diffErrorList);
        Result<Integer> result = diffErrorService.batchSaveDiffErrorList(request);
        if (Objects.nonNull(result) && result.isSuccess()) {
            return true;
        }
        return false;
    }
}
